package com.cyl.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.wms.domain.*;
import com.cyl.wms.mapper.*;
import com.cyl.wms.pojo.dto.PdaData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional
public class PdaService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ContainerMapper containerMapper;

    @Autowired
    private LoaderMapper loaderMapper;

    @Autowired
    private ContainerService containerService;
    @Autowired
    private LoaderService loaderService;

    @Autowired
    private InventoryHistoryMapper inventoryHistoryMapper;

    public PdaData stow(PdaData data) {
        String deviceCode = data.getDeviceCode();
        String containerCode = data.getContainerCode();
        //检查是哪个容器，可能扫描的是地址码、库位码、或
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemNo",deviceCode);
        Item item = itemMapper.selectOne(queryWrapper);

        QueryWrapper<Container> containerQueryWrapper = new QueryWrapper<>();
        containerQueryWrapper.eq("container_no",containerCode);
        Container container = containerMapper.selectOne(containerQueryWrapper);
        //可能从库位loader/料箱/地址码查询
        if(container == null){
            Loader loader =  loaderMapper.findByLoaderNo(containerCode);
            //如果loader为空 则通过addr创建loader
            if (loader==null) {
              loader = loaderService.createLoaderByAddress(containerCode);
            }
            if(loader != null){
                container = this.containerService.createContainerByLoader(loader);
            }
        }
        if (container == null){
            throw new RuntimeException("系统容器没有这个码位!" + containerCode);
        }

        //如果从待检库区找到该物料 表示为从检验完成的区域再重新入库，要将检验区该库存定义为0
        Inventory inCheck = this.inventoryMapper.findInventoryByCheckItem(item.getId());


        Inventory inventory = new Inventory();
        inventory.setItemId(item.getId());
        inventory.setQuantity(new BigDecimal(1));
        inventory.setContainerId(container.getId());

        inventoryMapper.insert(inventory);

        return data;
    }

    public PdaData pick(PdaData data) {
        String deviceCode = data.getDeviceCode();
        String containerCode = data.getContainerCode();
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemNo",deviceCode);
        Item item = itemMapper.selectOne(queryWrapper);

        //必然有了container
        QueryWrapper<Container> containerQueryWrapper = new QueryWrapper<>();
        containerQueryWrapper.eq("container_no",containerCode);
        Container container = containerMapper.selectOne(containerQueryWrapper);

        Inventory inventory = this.inventoryMapper.findInventoryByContainerAndItem(container.getId(),item.getId());
        inventory.setQuantity(new BigDecimal(0));

        this.inventoryMapper.updateById(inventory);

        return data;
    }

    public PdaData move(PdaData data) {

        return data;
    }

    public PdaData check(PdaData data) {

        return data;
    }
}
